#include "gas_station.h"

gas_station::gas_station(sc_module_name name): sc_module(name), fills(35), pump(2)
{
    // Process registrations
    SC_THREAD(customer1_thread);
    SC_THREAD(customer2_thread);
    SC_THREAD(customer3_thread);
    SC_METHOD(monitor_method);
    sensitive << e_change;
}

void gas_station::customer1_thread(void)
{
    customer("customer1");
}

void gas_station::customer2_thread(void)
{
    customer("customer2");
}

void gas_station::customer3_thread(void)
{
    customer("customer3");
}

void gas_station::customer(string who)
{
    while (true)
    {
        wait(10, SC_SEC);
        note(who, "Empty");
        if (!fills)
            break;
        pump.wait();
        e_change.notify();
        note(who, "Filling up");
        wait(10, SC_SEC);
        fills--;
        pump.post();
        e_change.notify();
        note(who, "Full");
    }
}

void gas_station::note(string who, string message)
{
    cout << "NOTE: " << sc_time_stamp() << ":" << who << ": "<< message << endl;
}

void gas_station::monitor_method(void)
{
    cout << "INFO: Monitor " << pump.get_value() << " available, with fills " << fills << endl;
}